const app = require('../express');
const teacher = require('../dao/teacherDao');
const bodyParser = require('body-parser');

// 添加老师
app.post('/addTeacher', bodyParser.json(), (req, res) => {
    let taccount = req.body.taccount;
    let tpwd = req.body.tpwd;
    let tname = req.body.tname;
    let classid = req.body.classid;
    teacher.insertTeacher(taccount, tpwd, tname, classid, () => {
        res.writeHead(200);
        res.end(JSON.stringify({msg:'添加老师成功'}));
    }, () => {
        res.end(JSON.stringify({msg:'新增老师失败'}));
    })
})

// 老师登录
app.post('/teacherLogin', bodyParser.json(), (req, res) => {
    let taccount = req.body.account;
    let tpwd = req.body.pwd;
    teacher.teacherLogin(taccount, tpwd, (result) => {
        if (result.length == 0) {
            res.end(JSON.stringify({
                msg: '账号或密码错误'
            }));
        }else{
            res.writeHead(200);
            res.end(JSON.stringify(result));
        }
    }, (err) => {
        res.end(JSON.stringify({
            msg: '账号或密码错误'
        }));
    })
})